package com.wzy.authority.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzy.authority.base.dto.SysUserDto;
import com.wzy.authority.base.request.PageParameter;
import com.wzy.authority.base.result.Results;
import com.wzy.authority.domain.SysRoleUser;
import com.wzy.authority.domain.SysUser;
import com.wzy.authority.mapper.SysRoleUserMapper;
import com.wzy.authority.mapper.SysUserMapper;
import com.wzy.authority.service.SysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @program: authorityManagement
 * @description: 用户服务
 * @author: 1
 * @create: 2020-03-14 10:14
 **/
@Service
@Transactional
public class SysUserServiceImpl implements SysUserService {


    @Resource
    private SysUserMapper sysUserMapper;


    @Resource
    private SysRoleUserMapper sysRoleUserMapper;

    /**
     * 分页查询用户数据
     *
     * @param pageParameter 分页参数
     * @param sysUserDto       条件封装对象
     * @return 用户列表
     */
    @Override
    public Results<Page<SysUser>> selectUserByPage(PageParameter pageParameter, SysUserDto sysUserDto) {
        PageHelper.startPage(pageParameter.getPageNum(), pageParameter.getPageSize());
        Page<SysUser> sysUsers = sysUserMapper.selectUserList(sysUserDto);
        return Results.success(sysUsers, sysUsers.getTotal());
    }


    @Override
    public Results<SysUser> saveUser(SysUserDto sysUserDto) {
        //保存用户数据  并返回userId
        sysUserMapper.insertSelective(sysUserDto);

        //保存用户角色信息
        SysRoleUser sysUserRole = new SysRoleUser();
        sysUserRole.setRoleid(sysUserDto.getRoleId());
        sysUserRole.setUserid(sysUserDto.getId());
        sysRoleUserMapper.insert(sysUserRole);

        return Results.success();
    }


    @Override
    public SysUser selectUserById(Integer user_id) {
        return sysUserMapper.selectByPrimaryKey(user_id);
    }


    @Override
    public Results updateSysUser(SysUserDto sysUserDto) {
        //先更新用户表
        sysUserMapper.updateByPrimaryKeySelective(sysUserDto);

        //如果存在权限就修改,如果不存在就新增
        SysRoleUser sysRoleUser = new SysRoleUser();
        sysRoleUser.setUserid(sysUserDto.getId());
        sysRoleUser.setRoleid(sysUserDto.getRoleId());
        //修改
        if (sysRoleUserMapper.selectRoleUserByUserId(sysUserDto.getId()) != null) {
            sysRoleUserMapper.updateByUserId(sysRoleUser);
        }
        //新增
        else {
            sysRoleUserMapper.insert(sysRoleUser);
        }
        return Results.success();
    }


    @Override
    public Results deleteUser(Integer userId) {

        //删除关联表中的数据
        sysRoleUserMapper.deleteByUserId(userId);

        //删除用户表中的数据
        int delNum = sysUserMapper.deleteByPrimaryKey(userId);

        if (delNum > 0) {
            return Results.success();
        } else {
            return Results.failure();
        }

    }

    @Override
    public SysUser selectUserByName(String username) {
        return sysUserMapper.selectUserByNane(username);
    }
}
